package com.example.travel.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.travel.common.AjaxResult;
import com.example.travel.common.AuthAccess;
import com.example.travel.common.Constants;
import com.example.travel.domain.entity.Orders;
import com.example.travel.domain.entity.Spot;
import com.example.travel.domain.entity.User;
import com.example.travel.domain.response.SpotTotalResponse;
import com.example.travel.mapper.OrdersMapper;
import com.example.travel.mapper.SpotMapper;
import com.example.travel.mapper.UserMapper;
import com.example.travel.service.SpotService;
import com.example.travel.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : LinQingLiang
 * @file : EchartsController.java
 * @Since : 2023/1/28 16:44
 */
@RestController
@RequestMapping("/echarts")
@Api(tags = "图表")
public class EchartsController {

    @Autowired
    private UserService userService;

    @Autowired
    private SpotService spotService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private SpotMapper spotMapper;

    @Resource
    private RedisTemplate redisTemplate;

//    @GetMapping("/example")
//    public AjaxResult getEcharts(){
//        Map<String,Object> map = new HashMap<>();
//        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
//        map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
//        return AjaxResult.success(map);
//    }

    @GetMapping("/members")
    public AjaxResult getMembers(){
        List<User> userList = userService.list();
//        对应四个季度
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for (User user:userList) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter){
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;
                default: break;
            }
        }
        return AjaxResult.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }


    @GetMapping("/getOrders")
    public AjaxResult getOrders(){
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("state","已支付");
        List<Orders> ordersList = ordersMapper.selectList(wrapper);
//        对应四个季度
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for (Orders orders:ordersList) {
            Date paytime = DateUtil.parseDateTime(orders.getPaymentTime());
            Quarter quarter = DateUtil.quarterEnum(paytime);
            switch (quarter){
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;
                default: break;
            }
        }
        return AjaxResult.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }


    @AuthAccess
//    @Cacheable(value = "spots", key = "'frontAll'")
    @GetMapping("/image/frontAll")
    public AjaxResult findAll(){
        //1.从缓存中获取数据
        String jsonStr = (String) redisTemplate.opsForValue().get(Constants.SPOTS_KEY);

        List<Spot> spotList;
        //2.取出来的数据是空的
        if (StrUtil.isBlank(jsonStr)){
            //3.从数据库查数据
            spotList = spotService.list();
            //4.在缓存到我们的redis当中
            redisTemplate.opsForValue().set(Constants.SPOTS_KEY, JSONUtil.toJsonStr(spotList));
        }else{
            //5.如果有，则去redis当中取数据
            spotList = JSONUtil.toBean(jsonStr, new TypeReference<List<Spot>>() {
            },true);
        }
        return AjaxResult.success(spotList);
    }


    @GetMapping("/total")
    public AjaxResult getSpotTotal(){
        SpotTotalResponse spotTotalResponse = new SpotTotalResponse();
        Long userCount = userMapper.selectCount(null);
        Long spotCount = spotMapper.selectCount(null);
        int orderCount = ordersMapper.selectOrderCount("已支付");
        int priceTotal = ordersMapper.selectTotalPrice("已支付");
        spotTotalResponse.setSpotTotal(spotCount);
        spotTotalResponse.setOrderTotal(orderCount);
        spotTotalResponse.setUserTotal(userCount);
        spotTotalResponse.setPriceTotal(priceTotal);
        return AjaxResult.success(spotTotalResponse);
    }


}
